package dht.storage;

import java.io.File;
import java.util.Vector;

import dht.IStorage;
import dht.entity.DataObject;
import dht.throwable.DhtException;
import dht.util.FileUtils;

public class StorageImpl implements IStorage {

	@Override
	public DataObject read(String blockName) throws DhtException {
		DataObject dataObject = new DataObject();
		dataObject.setBlockName(blockName);
		dataObject
				.setData(FileUtils.readFile(PathUtils.getBlockPath(blockName)));
		return dataObject;
	}

	@Override
	public void write(DataObject dataObject, boolean overwrite)
			throws DhtException {
		FileUtils.createFile(PathUtils.getBlockPath(dataObject.getBlockName()),
				dataObject.getData());
	}

	@Override
	public Vector<String> getFileNamesByVnodeIds(Vector<Integer> vnodeIds)
			throws DhtException {
		Vector<String> fileNames = new Vector<String>();
		for (int vnodeId : vnodeIds) {
			File dir = new File(PathUtils.getVnodePath(vnodeId));
			fileNames.addAll(FileUtils.getAllFiles(dir));
		}
		return fileNames;
	}

	@Override
	public void remove(String fileName) throws DhtException {
		// TODO Auto-generated method stub

	}
}
